package com.epalmpay.controller.manage.device;

import com.epalmpay.commom.BaseController;
import com.epalmpay.exception.ApplicationException;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.system.BaseOutDTO;
import com.epalmpay.dto.system.PageDTO;
import com.epalmpay.dto.terminalmanage.*;
import com.epalmpay.entity.Device;
import com.epalmpay.entity.DeviceDeskCardDetail;
import com.epalmpay.entity.DevicePos;
import com.epalmpay.entity.Page;
import com.epalmpay.service.common.IIndustryService;
import com.epalmpay.service.device.IDeviceService;
import com.epalmpay.service.operator.ISysLogService;
import com.epalmpay.util.Tools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping(value = "/manager/terminal")
public class TerminalManageController extends BaseController {

    @Resource
    private IIndustryService industryService;

    @Resource
    private IDeviceService deviceService;
    @Resource
    private ISysLogService sysLogService;

    /**
     * 显示用户分页列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public DeviceListOutDTO listTerminal(DeviceInDTO deviceInDTO, PageDTO pageDTO) {
        DeviceListOutDTO deviceListOutDTO = new DeviceListOutDTO();
        try {
            //Boolean tmp = deviceInDTO.getIsReload();
            //Integer tmp2 = deviceInDTO.getIndustryID();
            //String tmp3 = deviceInDTO.getLoginID();

            Page page = Tools.pageEmtity(pageDTO);
            //deviceListOutDTO.setPage(page);


            deviceListOutDTO.setIndustryList(industryService.getFirstLevel());
            //查询时取数据			
            if (deviceInDTO.getIsReload()) {
                //获取统计设备信息
                List<DeviceOutDTO> statisticsDeviceList = deviceService.
                        getStatisticsDeviceList(deviceInDTO.getGroupId(), deviceInDTO.getIndustryID(),
                                deviceInDTO.getJoinStart(), deviceInDTO.getJoinEnd(), deviceInDTO.getName(), page);
                deviceListOutDTO.setDeviceOutList(statisticsDeviceList);
            }
            //Page page=Tools.pageEmtity(pageDTO);
            deviceListOutDTO.setPage(page);
            deviceListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return deviceListOutDTO;
        } catch (ApplicationException ae) {
            deviceListOutDTO.setErrorCode(ae.errCode, ae.errMsg);
            ae.printStackTrace();
        } catch (Exception e) {
            deviceListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }
        return deviceListOutDTO;
    }


    /**
     * 提交数据
     *
     * @param deviceAddInDTO
     * @return
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public BaseOutDTO addDeviceInfo(@RequestBody DeviceAddInDTO deviceAddInDTO) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();

        deviceAddInDTO.setGmtCreate(new Date());
        deviceAddInDTO.setGmtModified(new Date());
        deviceAddInDTO.setDeskcardAddress(HOME_URL+"/api/pay/tkIndex");

        try {
            deviceService.addDeviceInfo(deviceAddInDTO);
            baseOutDTO.setErrorCode(Rescode.SUCCESS, "新增成功");

            String content = "新增设备信息";
            sysLogService.addSysLog(deviceAddInDTO.getUserOutDTO().getId(), deviceAddInDTO.getUserOutDTO().getUsername(), null, content, deviceAddInDTO.getUserOutDTO().getGroupId());

        } catch (Exception e) {
            baseOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }

        return baseOutDTO;
    }


    /**
     * 根据大商户获取代理商
     *
     * @param groupPosInDTO
     * @return
     */
    @RequestMapping(value = "/getAgentList")
    @ResponseBody
    public GroupPosListOutDTO getAgentList(@RequestBody GroupPosInDTO groupPosInDTO) {

        GroupPosListOutDTO groupPosListOutDTO = new GroupPosListOutDTO();

        try {

            List<GroupPostOutDTO> groupPostOutDTOList = deviceService.getAgentIdAndName(groupPosInDTO.getGroupId());
            groupPosListOutDTO.setGroupPostOutDTOList(groupPostOutDTOList);
            groupPosListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return groupPosListOutDTO;
        } catch (Exception e) {
            groupPosListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }

        return groupPosListOutDTO;
    }


    /**
     * 渠道大商户的设备列表信息
     *
     * @param groupPosInDTO
     * @param pageDTO
     * @return
     */
    @RequestMapping(value = "/groupPosList")
    @ResponseBody
    public GroupPosListOutDTO listGroupPos(GroupPosInDTO groupPosInDTO, PageDTO pageDTO) {
        GroupPosListOutDTO groupPosListOutDTO = new GroupPosListOutDTO();

        try {
            Page page = Tools.pageEmtity(pageDTO);

            //查询设备表
            List<GroupPostOutDTO> groupPostOutDTOList = deviceService.getDeviceList(groupPosInDTO, page);
//            page.setTotalResult(groupPostOutDTOList.size());
            groupPosListOutDTO.setPage(page);
            groupPosListOutDTO.setGroupPostOutDTOList(groupPostOutDTOList);
            groupPosListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");

            return groupPosListOutDTO;

        } catch (Exception e) {
            groupPosListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }

        return groupPosListOutDTO;
    }


    /**
     * 根据门店所属代理商查询对应未绑定的设备信息
     *
     * @return
     */
    @RequestMapping(value = "/selectByProxy")
    @ResponseBody
    public DeviceListOutDTO selectByProxy(DeviceInDTO deviceInDTO) {
        DeviceListOutDTO deviceListOutDTO = null;
        try {
            deviceListOutDTO = deviceService.selectByPrimaryKey(deviceInDTO);
            deviceListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return deviceListOutDTO;
        } catch (Exception e) {
            deviceListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }

        return deviceListOutDTO;
    }


    /**
     * 渠道大商户的设备列表信息
     *
     * @param groupPosInDTO
     * @return
     */
    @RequestMapping(value = "/getShopList")
    @ResponseBody
    public GroupPosListOutDTO listShop(@RequestBody GroupPosInDTO groupPosInDTO) {
        GroupPosListOutDTO groupPosListOutDTO = new GroupPosListOutDTO();

        try {
            //查询设备表
            List<GroupPostOutDTO> groupPostOutDTOList = deviceService.getShopList(groupPosInDTO);
            groupPosListOutDTO.setGroupPostOutDTOList(groupPostOutDTOList);
            groupPosListOutDTO.setErrorCode(Rescode.SUCCESS, "处理成功");
            return groupPosListOutDTO;

        } catch (Exception e) {
            groupPosListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "处理出现异常");
            e.printStackTrace();
        }

        return groupPosListOutDTO;
    }


    /**
     * 更新状态
     *
     * @param groupPosInDTO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateDeviceStatus")
    @ResponseBody
    public BaseOutDTO updateDeviceStatus(@RequestBody GroupPosInDTO groupPosInDTO) throws Exception {

        int i = deviceService.updateDevice(groupPosInDTO);

        BaseOutDTO baseOutDTO = new BaseOutDTO();

        if (i > 0) {
            i = deviceService.updateDevicePosByDeviceId(groupPosInDTO);
            if (i > 0) {
                baseOutDTO.setErrorCode(Rescode.SUCCESS);
                String content = "更新设备状态";
                sysLogService.addSysLog(groupPosInDTO.getUserOutDTO().getId(), groupPosInDTO.getUserOutDTO().getUsername(), null, content, groupPosInDTO.getUserOutDTO().getGroupId());

            } else {
                baseOutDTO.setErrorCode(Rescode.FAIL);
            }

        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }


    /**
     * 注销
     *
     * @param groupPosInDTO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateDevicePosStatus")
    @ResponseBody
    public BaseOutDTO updateDevicePosStatus(@RequestBody GroupPosInDTO groupPosInDTO) throws Exception {
        BaseOutDTO baseOutDTO = new BaseOutDTO();

        int i = deviceService.updateDevicePosStatusAndDeviceStatus(groupPosInDTO);
        if (i > 0) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
            String content = "设备注销";
            sysLogService.addSysLog(groupPosInDTO.getUserOutDTO().getId(), groupPosInDTO.getUserOutDTO().getUsername(), null, content, groupPosInDTO.getUserOutDTO().getGroupId());

        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    /**
     * 解绑
     *
     * @param groupPosInDTO
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateDeviceUnbind")
    @ResponseBody
    public BaseOutDTO updateDeviceUnbind(@RequestBody GroupPosInDTO groupPosInDTO) throws Exception {
        BaseOutDTO baseOutDTO = new BaseOutDTO();

        int i = deviceService.updateDeviceUnBind(groupPosInDTO);
        if (i > 0) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);

            String content = "设备解除绑定";
            sysLogService.addSysLog(groupPosInDTO.getUserOutDTO().getId(), groupPosInDTO.getUserOutDTO().getUsername(), null, content, groupPosInDTO.getUserOutDTO().getGroupId());

        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    /**
     * 更新商户ID
     *
     * @param groupPosInDTO
     * @return
     */
    @RequestMapping(value = "/updateDeviceParams")
    @ResponseBody
    public BaseOutDTO updateDeviceParams(@RequestBody GroupPosInDTO groupPosInDTO) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        int i = deviceService.updateDevice(groupPosInDTO);
        if (i > 0) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }


    /**
     * 更新deviceInfo
     *
     * @return
     */
    @RequestMapping(value = "/updateDeviceInfo")
    @ResponseBody
    public BaseOutDTO updateDeviceInfo(@RequestBody Device device) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        int i = deviceService.updateDeviceInfo(device);
        if (i > 0) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    /**
     * 更新devicePosInfo
     *
     * @return
     */
    @RequestMapping(value = "/updateDevicePosInfo")
    @ResponseBody
    public BaseOutDTO updateDevicePosInfo(@RequestBody DevicePos devicePos) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        int i = deviceService.updateDevicePosInfo(devicePos);
        if (i > 0) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }


    /**
     * 更新deviceSmInfo
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateDeviceSmInfo")
    @ResponseBody
    public BaseOutDTO updateDeviceSmInfo(@RequestBody DeviceAddInDTO deviceSm) throws Exception {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        int i = deviceService.updateDeviceSmInfo(deviceSm);
        if (i > 0) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);

            String content = "更新设备扫码信息";
            sysLogService.addSysLog(deviceSm.getUserOutDTO().getId(), deviceSm.getUserOutDTO().getUsername(), null, content, deviceSm.getUserOutDTO().getGroupId());

        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }


    /**
     * 更新deviceDeskInfo
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/updateDeviceDeskInfo")
    @ResponseBody
    public BaseOutDTO updateDeviceDeskInfo(@RequestBody DeviceAddInDTO deviceDesk) throws Exception {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        deviceDesk.setDeskcardAddress(HOME_URL+"/api/pay/tkIndex");
        int i = deviceService.updateDeviceDeskInfo(deviceDesk);
        if (i > 0) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);


            String content = "编辑台卡信息";
            sysLogService.addSysLog(deviceDesk.getUserOutDTO().getId(), deviceDesk.getUserOutDTO().getUsername(), null, content, deviceDesk.getUserOutDTO().getGroupId());
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }


    /**
     * 删除deviceSmInfo
     *
     * @return
     */
    @RequestMapping(value = "/delSmDetailsById")
    @ResponseBody
    public BaseOutDTO delSmDetailsById(@RequestBody Long id) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        int i = deviceService.delDeviceSmDetailsById(id);
        if (i > 0) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }

    /**
     * 删除deviceDeskInfo
     *
     * @return
     */
    @RequestMapping(value = "/delDeskDetailsById")
    @ResponseBody
    public BaseOutDTO delDeskDetailsById(@RequestBody Long id) {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        int i = deviceService.delDeviceDeskDetailsById(id);
        if (i > 0) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);
        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }


    @RequestMapping(value = "/getDeviceInfo")
    @ResponseBody
    public DeviceInfoOutDTO getDeviceInfo(@RequestBody DeviceInDTO deviceInDTO) {
        return deviceService.getDeviceInfo(deviceInDTO.getDeviceId());
    }


    @RequestMapping(value = "/getDeviceDeskCardDetail")
    @ResponseBody
    public DeviceDeskCardDetail getDeviceInfo(@RequestBody Long qrId) {
        return deviceService.getDeviceCardDetailById(qrId);
    }


    /**
     * 新增POS维修信息
     *
     * @return
     */
    @RequestMapping(value = "/addRepairInfo")
    @ResponseBody
    public BaseOutDTO addRepairInfo(@RequestBody DevicePosRepairInDTO devicePosRepairInDTO) throws Exception {
        BaseOutDTO baseOutDTO = new BaseOutDTO();
        int i = deviceService.addRepairInfo(devicePosRepairInDTO);
        if (i > 0) {
            baseOutDTO.setErrorCode(Rescode.SUCCESS);

            String content = "新增POS维修信息";
            sysLogService.addSysLog(devicePosRepairInDTO.getUserOutDTO().getId(), devicePosRepairInDTO.getUserOutDTO().getUsername(), null, content, devicePosRepairInDTO.getUserOutDTO().getGroupId());

        } else {
            baseOutDTO.setErrorCode(Rescode.FAIL);
        }
        return baseOutDTO;
    }


    /**
     * 根据替换后的POS_ID 查询原始信息
     *
     * @param oldPosId
     * @return
     */
    @RequestMapping(value = "/getDeviceInfoByReplacePosId")
    @ResponseBody
    public DeviceInfoOutDTO getDeviceInfoByReplacePosId(@RequestBody Long oldPosId) {
        return deviceService.getDeviceInfoByReplacePosId(oldPosId);
    }
    
    
    @RequestMapping(value = "/getDeviceByShopIdList")
    @ResponseBody
    public DeviceListOutDTO getDeviceByShopIdList(DeviceInDTO deviceInDTO) {
    	DeviceListOutDTO deviceListOutDTO=new DeviceListOutDTO();
        try {
			return deviceService.getByDeviceShopId(deviceInDTO.getShopId());
		} catch (ApplicationException e) {
			e.printStackTrace();
			deviceListOutDTO.setErrorCode(Rescode.DEFAULT_PREFIX, "查询异常");
		} 
		return deviceListOutDTO;
    }

}
